﻿using System;
using RG.Common.Configuration;
using Microsoft.Practices.Unity;

namespace RG.Common.WPF.Filters.ToolBarFilters
{
    /// <summary>
    /// Фильтр по дате
    /// </summary>
    [SupportDataBinding]
    public class FilterDateFrameVM : FilterBaseParamVM<DatesRange>
    {
        public FilterDateFrameVM(IUnityContainer cfg):base(cfg)
        {
            Name = "";
            FromDate = DateTime.Now.AddDays(-14).Date;
            ToDate = DateTime.Now.Date;
            NLastDays = 14;
        }

        public bool IsDetailedPeriod { get; set; }
        public bool IsShortPeriod { get { return !IsDetailedPeriod; } }

        public string FormattedValue
        {
            get
            {
                var temp = ToDatesRange();
                if (temp == null || !IsUsed) return EmptyText;
                string fmt = IsDetailedPeriod ? "dd.MM.yyyy HH:mm" : "dd.MM.yyyy";
                return string.Format("от {0}\r\nдо {1}", 
                    temp.minDate.Value.ToString(fmt),
                    temp.maxDate.Value.ToString(fmt));
            }
        }


        private DateTime fromDate;

        [PropertySetting]
        public DateTime FromDate
        {
            get { return fromDate; }
            set
            {
                fromDate = value;
                SetValue();
            }
        }

        private DateTime toDate;

        [PropertySetting]
        public DateTime ToDate
        {
            get { return toDate; }
            set
            {
                toDate = value;
                SetValue();
            }
        }

        private int nLastDays;

        [PropertySetting]
        public int NLastDays
        {
            get { return nLastDays; }
            set
            {
                nLastDays = value;
                SetValue();
            }
        }

        private bool isLastDays;

        [PropertySetting]
        public bool IsLastDays
        {
            get { return isLastDays; }
            set
            {
                isLastDays = value;
                SetValue();
                SetUsage();
            }
        }

        private bool isUsed;

        [PropertySetting]
        public bool IsUsed
        {
            get { return isUsed; }
            set
            {
                isUsed = value;
                SetValue();
            }
        }

        private bool isPeriod;

        [PropertySetting]
        public bool IsPeriod
        {
            get { return isPeriod; }
            set
            {
                isPeriod = value;
                SetValue();
                SetUsage();
            }
        }

        private bool isBefore;

        [PropertySetting]
        public bool IsBefore
        {
            get { return isBefore; }
            set
            {
                isBefore = value; 
                SetValue();
                SetUsage();
            }
        }

        private void SetValue()
        {
            Value = ToDatesRange();
        }
        
        private void SetUsage()
        {
            if (!IsBefore && !IsPeriod && !IsLastDays)
                IsUsed = false;
            else IsUsed = true;
        }

        public override void ClearValueQuiet()
        {
            IsBefore = false;
            IsPeriod = false;
            IsLastDays = false;
            IsUsed = false;
        }

        public override bool ValueEmpty()
        {
            return  !IsUsed;
        }

        public DatesRange ToDatesRange()
        {
            var result = new DatesRange();
            if (IsUsed)
            {
                if (IsLastDays)
                {
                    result.minDate = DateTime.Now.AddDays(-NLastDays).Date;
                    result.maxDate = DateFilterType.LastKnownDate.Date;
                }
                if (IsPeriod)
                {
                    result.minDate = IsDetailedPeriod ? FromDate : FromDate.Date;
                    result.maxDate = IsDetailedPeriod ? ToDate : ToDate.Date;
                }
                if (IsBefore)
                {
                    result.minDate = DateTime.Now.AddYears(-50).Date;
                    result.maxDate = ToDate.Date;
                }
            }
            return IsLastDays || IsPeriod || IsBefore ? result : null;
        }

        protected override DatesRange ReturnDefaultEmptyValue()
        {
            return null;
        }
    }
}